package com.hsyt.pms.sys.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pms.api.dto.org.OrgDto;
import com.hsyt.pms.api.dto.system.SysRoleDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.enums.SourceType;
import com.hsyt.pms.api.vo.system.SysRoleVo;
import com.hsyt.pms.api.vo.system.TreeNode;
import com.hsyt.pms.orm.sys.SysRoleDao;
import com.hsyt.pms.sys.dubbo.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: SysRoleServiceImpl
 * @Description: 角色服务实现类
 * @author wangze@hsyuntai.com
 * @date 2019年2月12日 下午2:45:51
 */
@Slf4j
@Component
@Service(version = "0.0.1", interfaceClass = SysRoleService.class, registry = "pms", group = "pmsSystem", timeout = 3000)
public class SysRoleServiceImpl implements SysRoleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Resource
    private SysRoleDao pmsRoleDao;

    @Override
    public void addRole(SysRoleDto pmsRoleDto) {
        pmsRoleDao.insert(pmsRoleDto);
    }

    @Override
    public void updateRoleByKeySelective(SysRoleDto role) {
        pmsRoleDao.updateByPrimaryKeySelective(role);
    }

    @Override
    public int countRole(SysRoleDto po) {
        return pmsRoleDao.countRole(po);
    }

    @Override
    public PageResult<SysRoleVo> searchPageDataByDto(SysRoleDto dto) {
        Page<?> page = PageHelper.startPage(dto.getPageNumber(), dto.getPageSize(), true);
        List<SysRoleVo> list = pmsRoleDao.queryRoleByDto(dto);
        PageResult<SysRoleVo> pageResult = PageResult.create(page.getTotal(), list);
        return pageResult;
    }

    @Override
    public List<SysRoleVo> searchRoleByOrg(Long orgId) {
        List<SysRoleVo> list = pmsRoleDao.searchRoleByOrg(orgId);
        return list;
    }

    @Override
    public List<TreeNode> getOrgTree(Long userId, Long roleId, OrgDto orgDto) {
        return pmsRoleDao.getOrgTreeById(userId, roleId, orgDto);
    }

    @Override
    public void saveMenus(SysRoleDto pmsRoleDto) {
        // 0: 解析角色数据
        String[] roles = StringUtils.split(StringUtils.replace(pmsRoleDto.getRole(), "[", "").replace("]", ""), ",");
        if (null == roles) {
            return;
        }
        // 1: 菜单及功能授权保存
        String[] newStrs = StringUtils.split(pmsRoleDto.getNewStr(), ",");
        String[] delStrs = StringUtils.split(pmsRoleDto.getDelStr(), ",");
        if (null == newStrs && null == delStrs) {
            return;
        }
        for (String roleId : roles) {
            SysRoleDto dto = null;
            for (String newStr : newStrs) {
                dto = new SysRoleDto();
                dto.setRoleId(Long.valueOf(roleId));
                dto.setFunctionId(Long.valueOf(newStr));
                dto.setStatus(AbleStatus.ENABLED.getCode());
                pmsRoleDao.insertRoleMenu(dto);
            }
            for (String delStr : delStrs) {
                dto = new SysRoleDto();
                dto.setRoleId(Long.valueOf(roleId));
                dto.setFunctionId(Long.valueOf(delStr));
                pmsRoleDao.deleteByRoleAndFun(dto);
            }
        }
    }

    @Override
    public void saveOrgRole(SysRoleDto pmsRoleDto) {
        // 0: 解析角色数据
        String[] roles = StringUtils.split(StringUtils.replace(pmsRoleDto.getRole(), "[", "").replace("]", ""), ",");
        if (null == roles) {
            return;
        }
        // 1: 组织授权保存
        String[] newStrs = StringUtils.split(pmsRoleDto.getNewStr(), ",");
        String[] delStrs = StringUtils.split(pmsRoleDto.getDelStr(), ",");
        saveGrantRole(pmsRoleDto, newStrs, delStrs, roles, SourceType.ORG.getValue());
    }

    @Override
    public void saveSourceRole(SysRoleDto pmsRoleDto) {
        // 0: 解析角色数据
        String[] roles = StringUtils.split(StringUtils.replace(pmsRoleDto.getRole(), "[", "").replace("]", ""), ",");
        if (null == roles) {
            return;
        }
        // 1: 建筑物授权保存
        String[] newStrs = StringUtils.split(pmsRoleDto.getNewStr(), ",");
        String[] delStrs = StringUtils.split(pmsRoleDto.getDelStr(), ",");
        if (null != newStrs || null != delStrs) {
            saveGrantRole(pmsRoleDto, newStrs, delStrs, roles, SourceType.BUILDING.getValue());
        }
        // 2: 设备授权保存
        String[] newStrsSheBeiDian = StringUtils.split(pmsRoleDto.getNewStrSheBei(), ",");
        String[] delStrsSheBeiDian = StringUtils.split(pmsRoleDto.getDelStrSheBei(), ",");
        if (null != newStrsSheBeiDian || null != delStrsSheBeiDian) {
            saveGrantRole(pmsRoleDto, newStrsSheBeiDian, delStrsSheBeiDian, roles, SourceType.SENSOR.getValue());
        }
        // 3: 设备片区授权保存
        String[] newStrsSheBeiQu = StringUtils.split(pmsRoleDto.getNewStrSheBeiQu(), ",");
        String[] delStrsSheBeiQu = StringUtils.split(pmsRoleDto.getDelStrSheBeiQu(), ",");
        if (null != newStrsSheBeiQu || null != delStrsSheBeiQu) {
            saveGrantRole(pmsRoleDto, newStrsSheBeiQu, delStrsSheBeiQu, roles, SourceType.DEVICEAREA.getValue());
        }
    }

    public void saveGrantRole(SysRoleDto pmsRoleDto, String[] newStrs, String[] delStrs, String[] roles, Integer SourceType) {
        for (String roleId : roles) {
            SysRoleDto dto = null;
            for (String newStr : newStrs) {
                dto = new SysRoleDto();
                dto.setSourceId(Long.valueOf(newStr));
                dto.setSourceType(SourceType);
                dto.setSourceCode("ROLE_DATA_" + SourceType + "_" + dto.getSourceId());
                dto.setRoleId(Long.valueOf(roleId));
                dto.setStatus(AbleStatus.ENABLED.getCode());
                dto.setAuthType(pmsRoleDto.getAuthType());
                pmsRoleDao.insertRoleSource(dto);
            }
            for (String delStr : delStrs) {
                dto = new SysRoleDto();
                dto.setRoleId(Long.valueOf(roleId));
                dto.setSourceId(Long.valueOf(delStr));
                dto.setSourceType(SourceType);
                pmsRoleDao.deleteRoleSource(dto);
            }
        }
    }

}
